//
//  LoginViewController.m
//  WuLi
//
//  Created by Gollum on 16/9/18.
//  Copyright © 2016年 Gollum. All rights reserved.
//

#import "LoginViewController.h"
#import "NetworkTool.h"
#import "NSString+MD5.h"
#import "MineViewController.h"

#import "LoginAndRegisterModel.h"
#import "LoginAndRegisterData.h"


@interface LoginViewController ()
@property (weak, nonatomic) IBOutlet UIButton *forgetPW;
@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;
@property (weak, nonatomic) IBOutlet UITextField *passWordTextField;
@property (weak, nonatomic) IBOutlet UIButton *loginButton;


@property (nonatomic,strong)LoginAndRegisterModel *loginModel;
@property (nonatomic,strong)NSMutableArray *dataArray;



@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _dataArray = [NSMutableArray array];
    _forgetPW.clipsToBounds = YES;
    _forgetPW.layer.cornerRadius = self.forgetPW.frame.size.height / 2;
    _loginButton.clipsToBounds = YES;
    _loginButton.layer.cornerRadius = self.loginButton.frame.size.height / 2;
    
}
- (void)viewWillAppear:(BOOL)animated{

    [_phoneTextField becomeFirstResponder];

}

- (void)handleData{
    
    NSString *urlString = @"http://login.5wuli.com/v1/user/login";
    
    NSDictionary *header = @{@"User-Agent":@"Dalvik/2.1.0 (Linux; U; Android 5.0; ASUS_Z00ADB Build/LRX21V) Wuli/2.3.1 (agent:s;channel:wandoujia;credential:e1MyMDEwMDAxMDF9LXsjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIzM1Nzg3MjA2MTcxNDM4M30teyMjIyMjIyMxNDcyMDAyMTMyOTYzLTJlZjRjNThjZDJ9QDE0NzExODgxOTgzODY=;deviceId:357872061714383;osTypeId:01;detailInfo:android;simTypeId:01;netTypeId:01;deviceTypeId:01;osVersion:5.0;idfa:null)", @"Host":@"login.5wuli.com", @"Content-Type":@"application/json;charset=utf-8", @"Accept-Encoding":@"gzip", @"Content-Length":@"74"};
    NSString *md5String = [_passWordTextField.text stringWith32BitMD5Lower];
    NSDictionary *body = @{@"mobile":_phoneTextField.text, @"pwd":md5String};
    [NetworkTool POST:urlString andBody:body andBodyStyle:JHBodyJSON andHeader:header andResponse:JHJSON andSuccessBlock:^(id result) {
        
//        NSLog(@"------------------%@",result);
        
        _loginModel = [LoginAndRegisterModel modelWithDic:result];
        [_dataArray addObject:_loginModel];
        
        
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        //登陆成功后把用户名和密码存储到UserDefault
        [userDefaults setObject:_loginModel.data.nickName forKey:@"userName"];
        [userDefaults setObject:_passWordTextField.text forKey:@"passWord"];
        [userDefaults setObject:_loginModel.data.portrait forKey:@"userImageUrl"];
        
        [userDefaults synchronize];

        
        
        
    } andFailureBlock:^(NSError *error) {
        
    }];
}
//登录请求数据类型

//{
//    "requestId": "2147d85c-a16c-485c-8662-2428d9682338",
//    "requestStartTime": 1474341470949,
//    "requestEndTime": 1474341470949,
//    "code": "10000",
//    "data": {
//        "uId": "504658ec2ea9e0e70582ea07eb7a799e",
//        "credential": "e1MyMDIwMDAxMDF9LXsjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIzM1Nzg3MjA2MTcxNDM4M30teyMjIyMjIyMxNDcyMDA5NzE2NTcwLTU0OTIyZjhiZTV9QDE0NzM1Mjc1MzYzNTI=",
//        "nickName": "18341893505",
//        "portrait": "http://meta.9liuda.com/image/user/icon/center_default_icon48.png",
//        "mobile": "18341893505",
//        "sex": null,
//        "birthday": null,
//        "appId": null,
//        "createTime": null,
//        "lastLoginTime": 1474341398000,
//        "totalLoginCount": 153,
//        "partnerInfoMap": null,
//        "ageGroup": null,
//        "profession": null
//    },
//    "message": "ok",
//    "attached": null
//}






- (IBAction)forgetPW:(id)sender {
    
    
}

- (IBAction)loginButton:(id)sender {
    
    [self handleData];
    //获取用户输入的信息
//    NSString *username = self.phoneTextField.text;
//    NSString *password = self.passWordTextField.text;
//    //对用户信息的验证
//    if ([username isEqualToString:password]){
//        //获取userDefault单例
//        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
//        //登陆成功后把用户名和密码存储到UserDefault
//        [userDefaults setObject:username forKey:@"name"];
//        [userDefaults setObject:password forKey:@"password"];
//        [userDefaults synchronize];
//    }
    
//    MineViewController *mine = [[MineViewController alloc] init];
//    [self presentModalViewController:mine animated:YES];
//    //返回
//    [self dismissModalViewControllerAnimated:YES];
    
}

- (IBAction)handleBack:(id)sender {
    
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
